/*
 * @ClassName base_api.dart
 * @Description base_api
 * @Author SuPeiSen
 * @Date 2020/7/14 15:38
 */

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_su/common/show.dart';
import 'package:flutter_su/model/base_model.dart';
import 'package:json_annotation/json_annotation.dart';

//typedef toJsonCallBack = int (){}

mixin BaseApi{
  final CancelToken cancel = CancelToken(); // 取消请求对象
  final Object nullModel = null;  // 请求不成功返回null

  // 取消当前的网络请求, 不会自动取消弹窗
  void showLoadingIconOnTap(){
    cancel.cancel("取消");
  }

  // 对toJsonModel进行校验
  T checkToModel<T extends BaseModel>({@required dynamic obj, @required Map map}){
    try{
      T object =  obj.toModel<T>(map) as T;
      return object;
    } on NoSuchMethodError{
      Show.error(msg: "方法不存在");
      return null;
    } catch(e){
      Show.error(msg: "数据异常");
      return null;
    }
  }
}

